<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <title>课程-课堂提问任务-发布</title>

    <%@ include file="/common/commonLib.jsp" %>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/taskPublish.css'/>"/>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/common-main.css'/>"/>
    <script src="<c:url value='/js/taskClassroomPublish.js'/>" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="container-fluid main">
    <div class="row">
        <div class="col-md-12">
            <%@include file="/common/header.jsp" %>
        </div>
        <div class="col-md-12">
            <%@include file="common/courseHeader.jsp" %>
        </div>
        <div class="col-md-12">
            <div class="row">
                <div class="col-md-3">
                    <%@include file="common/courseLeft.jsp" %>
                </div>
                <div class="col-md-9">
                    <div class="row">
                        <div class="col-md-12">
                            <%@include file="common/courseNavbar.jsp" %>
                        </div>
                        <div class="col-md-12">
                            <div class="tab-nav">
                                <a id="lesson-task-pub-href1" class="front-nav">任务首页
                                </a>&nbsp;>&nbsp;<a id="lesson-task-pub-href2" class="front-nav">任务详情
                            </a>&nbsp;>&nbsp;<span class="back-nav">任务分配记录</span>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input type="button" id="new-lesson-task-publish" class="btn btn-primary" value="分配任务">
                        </div>
                        <div class="col-md-12">
                            <label for="">任务分配记录</label>
                            <!-- 课堂提问发布记录 -->
                            <ul id="task-pub-record-classroom">

                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 添加任务分配对象 -->
<div class="row" id="add-lesson-publish-object">
    <div class="col-md-11">
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-2">
                            <label for="">选择班级</label>
                        </div>
                        <div class="col-xs-4">
                            <select class="form-control" id="classList" onchange="selectTaskLessonClass(this)">

                            </select>
                        </div>
                        <div class="col-xs-6">
                            <label>该班人数为:</label>
                            <span id="currentClassNumber"></span>
                            <span style="color:red;">*设置提问人数时请注意！</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-2">
                            <label for="">选择课堂</label>
                        </div>
                        <div class="col-xs-10">
                            <div class="publish-classroom-ready">

                                <ul id="lessonList">

                                </ul>
                            </div>
                            <div class="page-content">
                                <nav class="pagination" role="navigation">
                                    <a class="last-page" id="lastPageInLesson">
                                        <i class="fa fa-angle-left"></i>
                                    </a>
                                    <span class="page-number">
											第<span id="pageNowInLesson"></span>页/共<span id="pageNumInLesson"></span>页
										</span>
                                    <a class="next-page" id="nextPageInLesson">
                                        <i class="fa fa-angle-right"></i>
                                    </a>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-2">
                            <label for="">已选课堂</label>
                        </div>
                        <div class="col-xs-10">
                            <div class="publish-classroom-has">

                                <ul id="chooseList">

                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-12" style="text-align:center;margin-top:2rem;margin-bottom:4rem;">
                <button type="button" id="taskLessonPublishBtn" class="btn btn-primary">发布</button>
            </div>
        </div>
    </div>
    <div class="col-md-1"></div>
</div>

<!-- 课堂提问学生名单 -->
<div class="row" id="task-lesson-student">
    <div class="col-xs-1"></div>
    <div class="col-xs-10" style="text-align:center;">
        <table class="table table-hover">
            <thead>
            <tr>
                <th>班级</th>
                <th>学号</th>
                <th>姓名</th>
                <th>分数</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="scorePerformanceList">

            </tbody>
        </table>
    </div>
    <div class="col-xs-1"></div>
</div>
<%@include file="/common/footer.jsp" %>
</body>
<script>
    var data =${data};
    var classs = [];
    var lessons = [];
    var chooses = [];
    var courseId;
    var taskId;
    $(document).ready(function () {
        if (data.status != "success") {
            alert(data.message);
            return;
        }
        $("#task").css("color", "#00b43c");
        courseId = data.data.courseId;
        changeUrl(data.data.courseId);
        taskId = data.data.taskId;
        $("#taskLessonPublishBtn").click(publishTask);
        fetchCourse(data.data.courseId);
        initAssignList();
        $("#lesson-task-pub-href1").attr("href", CONTEXT_PATH + "task/list?courseId=" + data.data.courseId);
        $("#lesson-task-pub-href2").attr("href", CONTEXT_PATH + "task/detail?id=" + data.data.taskId);
    });

    var selectTaskLessonClass = function (value) {
        var selectedOption = value.options[value.selectedIndex];
        getLessons(selectedOption.value);
    }
    var initAssignList = function () {
        var list = $("#task-pub-record-classroom");
        list.empty();
        if (data.data.list.length <= 0) {
            var s = "<li>（无分配记录）</li>";
            list.append(s);
        } else {
            for (var i = 0; i < data.data.list.length; i++) {
                var item = data.data.list[i];
                var s = "<li>";
                s += "<span onclick=\"displayStudent(" + item.id + ")\" title=\"" + item.className + ">课堂:" + item.beginDate + "--" + item.endDate + ">地点:" + item.location + "\">" + item.className + ">课堂:" + item.beginDate + "--" + item.endDate + ">地点:" + item.location + "</span>";
                if (item.isAnswerVisible == "YES") {
                    s += "<input type=\"button\" class=\"btn btn-success publish-answer\" onclick=\"showAnswer(" + item.id + ",'NO')\" value=\"取消公布\">";
                } else {
                    s += "<input type=\"button\" class=\"btn btn-info publish-answer\" onclick=\"showAnswer(" + item.id + ",'YES')\" value=\"公布答案\">";
                }
                s += "<input type=\"button\" onclick=\"delete1(" + item.id + ") \" class=\"btn btn-danger publish-answer\" value=\"删除\">";
                s += "</li>";
                list.append(s);
            }
        }
    };

    function delete1(id) {
        console.log("id:::" + id);
        if (window.confirm("您确定要删除吗?")) {
            $.post(CONTEXT_PATH + "taskAssignment/ajaxDelete", "id=" + id, function (data) {
                if (data.status == "success") {
                    layer.msg("删除成功");
                    location.reload();
                } else {
                    layer.msg("删除失败");
                }
            });
        }

    }

    var getClassList = function () {
        var json = {
            courseId: data.data.courseId,
            classType: "CLASS"
        }
        $.get(CONTEXT_PATH + "class/ajaxListData", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                initClassList(data.data.list);
            } else {
                alert(data.message);
            }
        });
    }

    var initClassList = function (list) {
        classs = list;
        var classList = $("select[id='classList']");
        classList.empty();
        for (var i = 0; i < list.length; i++) {
            var str = "";
            if (i == 0) {
                str += ("<option selected=\"selected\" value=\"" + list[i].id + "\">" + list[i].name + "</option>");
                getLessons(list[i].id);
            } else {
                str += ("<option value=\"" + list[i].id + "\">" + list[i].name + "</option>");
            }
            classList.append(str);
        }
    }

    var getLessons = function (classId) {
        if (isEmpty(classId)) {
            classId = $("#classList option:selected").val();
        }
        var json = {
            classId: classId,
            lessonType: "THEORY",
            taskId: data.data.taskId
        }
        $.get(CONTEXT_PATH + "taskAssignment/assignLesson", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                initLesson(data.data.lessons);
                initStudentNumberOfClass(data.data.number);
            } else {
                alert(data.message);
            }
        });
    }
    var initStudentNumberOfClass = function (number) {
        $("#currentClassNumber").text(number);
    }
    var initLesson = function (list) {
        var begin = 0;
        var end = 5;
        var pageNow = 1;
        var pageNum;
        if (list.length < 5) {
            end = list.length;
            pageNum = 1;
            pageNow = 1;
        } else {
            pageNum = Math.ceil(list.length / 5);
            begin = (pageNow - 1) * 5;
        }
        var pageNow1 = $("#pageNowInLesson").text();
        $("#lastPageInLesson").click(function () {
            if (pageNow1 == 1 || pageNow == 1) {
                pageNow = 1;
                begin = 0;
                layer.msg('已经是第一页啦！');
            } else {
                pageNow--;
                begin = (pageNow - 1) * 5;
            }
            end = pageNow * 5;
            $("#pageNowInLesson").text(pageNow);
            $("#pageNumInLesson").text(pageNum);
            console.log(begin + " " + end);
            pageList(list, begin, end);
        });
        $("#nextPageInLesson").click(function () {
            if (pageNow1 == pageNum || pageNow == pageNum) {
                pageNow = pageNum;
                end = list.length;
                layer.msg('已经是最后一页啦！');
            } else {
                pageNow++;
                if (pageNow == pageNum) {
                    end = list.length;
                } else {
                    end = pageNow * 5;
                }
            }
            begin = (pageNow - 1) * 5;
            $("#pageNowInLesson").text(pageNow);
            $("#pageNumInLesson").text(pageNum);
            pageList(list, begin, end);
        });
        $("#pageNowInLesson").text(pageNow);
        $("#pageNumInLesson").text(pageNum);
        pageList(list, begin, end);
    }

    var pageList = function (list, begin, end) {
        var lessonList = $("#lessonList");
        lessons = list;
        lessonList.empty();
        for (var i = begin; i < end; i++) {
            var s = "";
            if (contain(list[i])) {
                s += "<li>";
                s += "<span title=\"课堂:" + list[i].beginDate + "--" + list[i].endDate + "  上课地点:" + list[i].location + "\">课堂:" + list[i].beginDate + "--" + list[i].endDate + "  上课地点:" + list[i].location + "</span>";
                s += "<input type=\"button\" disabled=\"disabled\" class=\"btn btn-primary add-lesson\" value=\"添加\">";
                s += "</li>";
                lessonList.append(s);
            } else {
                s += "<li>";
                s += "<span title=\"课堂:" + list[i].beginDate + "--" + list[i].endDate + "  上课地点:" + list[i].location + "\">课堂:" + list[i].beginDate + "--" + list[i].endDate + "  上课地点:" + list[i].location + "</span>";
                s += "<input type=\"button\" class=\"btn btn-primary add-lesson\" onclick=\"initChooseList(" + i + ",this)\" value=\"添加\">";
                s += "</li>";
                lessonList.append(s);
            }
        }
    }

    var initChooseList = function (i, obj) {
        var chooseList = $("#chooseList");
        var item = lessons[i];

        var s = "";
        s += "<li>";
        s += "<span title=\"" + item.beginDate + "--" + item.endDate + "  " + item.location + "\">" + item.beginDate + "--" + item.endDate + "  " + item.location + "</span>";

        s += "<p class=\"radio-cho\" ><input type=\"radio\" name=\"pub" + chooses.length + "\" value=\"NO\" onclick=\"isPublish(" + chooses.length + ")\" checked >预发布</p>";
        s += "<p class=\"radio-cho\" ><input type=\"radio\" name=\"pub" + chooses.length + "\" value=\"YES\" onclick=\"isPublish(" + chooses.length + ")\">立即发布</p>";
        s += "<input type=\"button\" class=\"btn btn-danger del-lesson\" onclick=\"deletechooseLesson(" + i + ",this)\" value=\"删除\">";
        s += "<label>提问人数：</label>"
        s += "<input type=\"number\" class=\"form-control\" min=\"1\" name=\"numsOfAnswerers\" value=\"1\" >";
        s += "<label name=\"publishTimeLabel" + chooses.length + "\">公开时间：</label>";
        s += "<input type=\"datetime-local\" name=\"publishTime" + chooses.length + "\" class=\"form-control\" >";
        s += "</li>";
        chooseList.append(s);
        chooses.push(item);
        $(obj).attr("disabled", "disabled");
    }

    function isPublish(num) {
        console.log("函数执行");
        if ($("input[name='pub" + num + "']:checked").val() == "NO") {
            console.log("预发布");
            $("input[name='publishTime" + num + "']").show();
            $("label[name='publishTimeLabel" + num + "']").show();
        } else {
            console.log("发布");
            $("input[name='publishTime" + num + "']").hide();
            $("label[name='publishTimeLabel" + num + "']").hide();
            $("input[name='publishTime" + num + "']").val("");
        }
    }

    var deletechooseLesson = function (i, obj) {
        var item = lessons[i];
        $(obj).parent("li").remove();
        chooses.pop(item);
        initLesson(lessons);
    }
    var contain = function (item) {
        for (var i = 0; i < chooses.length; i++) {
            if (chooses[i].id == item.id) {
                return true;
            }
        }
        return false;
    }

    var publishTask = function () {

        var param = [];
        if (chooses.length <= 0) {
            alert("请选择待发布课堂");
            return;
        }
        var numsOfAnswerers = $("input[name='numsOfAnswerers']");
        for (var i = 0; i < chooses.length; i++) {
            var isVisible = $("input[name='pub" + i + "']:checked").val();
            if (isEmpty(isVisible)) {
                isVisible = "NO";
            }

            var numsOfAnswerer = numsOfAnswerers[i].value;
            if (isEmpty(numsOfAnswerer)) {
                alert("请输入提问人数");
                return false;
            }

			//任务预发布 选择任务公布时间

			var publishTime = $("input[name='publishTime" + i + "']").val();
			if(isVisible=="NO"){
				if (isEmpty(publishTime)) {
					alert("请选择任务公布日期！");
					return false;
				}
				publishTime = parseDate(publishTime);
			} else {
				// publishTime = "";
			}
            var item = {
                lessonId: chooses[i].id,
                isVisible: isVisible,
                numsOfAnswerers: numsOfAnswerer,
				publishDatetime:publishTime
            };
            param.push(item);
        }

        var json = {
            taskId: taskId,
            items: param
        };
        $.post(CONTEXT_PATH + "taskAssignment/ajaxBatchCreate", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    };

    var showAnswer = function (id, isVisible) {
        var json = {
            id: id,
            isVisible: isVisible
        };
        $.post(CONTEXT_PATH + "taskAssignment/ajaxUpdateAnswer", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    };

    var isEmpty = function (s) {
        if (s == null || s == "" || s == undefined) {
            return true;
        }
        return false;
    };

    var scorePerformanceList = function (id) {

        $.get(CONTEXT_PATH + "scorePerformance/ajaxListViewData?id=" + id, function (data) {
            if (data.status == "success") {
                var spList = $("#scorePerformanceList");
                spList.empty();
                for (var i = 0; i < data.data.list.length; i++) {
                    var item = data.data.list[i];
                    var s = "<tr>";
                    s += "<td title=\"" + item.classNumber + "\">" + item.classNumber + "</td>";
                    s += "<td title=\"" + item.studentCode + "\">" + item.studentCode + "</td>";
                    s += "<td title=\"" + item.askStuName + "\">" + item.askStuName + "</td>";
                    s += "<td><input type=\"number\" name=\"mark" + i + "\" value=\"" + item.mark + "\" class=\"form-control\"></td>";
                    if (item.isScore == "NO") {
                        s += "<td><input type=\"button\" class=\"btn btn-primary\" value=\"评分\" onclick=\"scorePerformance(" + item.id + "," + i + ",this)\"></td>";
                    } else {
                        s += "<td><input type=\"button\" class=\"btn btn-primary\" value=\"再评分\" onclick=\"scorePerformance(" + item.id + "," + i + ",this)\"></td>";
                    }
                    s += "</tr>";
                    spList.append(s);
                }
            } else {
                alert(data.message);
            }
        });


    }

    //单个评分
    var scorePerformance = function (id, index, obj) {

        var input = $("input[name=\"mark" + index + "\"]");
        if (input.val() < 0 || input.val() > 100) {
            alert("分数必须在0~100之间！");
        } else {
            var json = {//获取要评分的学生成绩
                id: id,
                mark: input.val()
            };
            $.post(CONTEXT_PATH + "scorePerformance/scorePerform", {
                data: JSON.stringify(json)
            }, function (data) {
                if (data.status == "success") {
                    alert("评分成功！");
                    /* $(obj).attr("disabled","disabled");
                    $(obj).val("已评分"); */
                } else {
                    alert(data.message);
                }
            });
        }

    }

</script>
</html>